package com.plian.system.controller.tasksMsg;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.StringUtil;
import com.plian.system.annotation.Behavioral;
import com.plian.system.dto.tasksMsg.ScheduledTasksMsgDTO;
import com.plian.system.entity.ms.RectificationSituation;
import com.plian.system.entity.taskMsg.ScheduledTasksMsg;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.tasksMsg.IScheduledTasksMsgService;
import com.plian.system.vo.ms.RectificationSituationVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;


/**
 * 定时任务消息表
 * @author zhangsk
 * @create 2025-02-17 10:50
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("api/scheduledTasksMsg")
public class ScheduledTasksMsgController {
    private final static  String moduleType="zgqk";
    private IScheduledTasksMsgService iScheduledTasksMsgService;
    @PostMapping("/list")
    public ListResult<ScheduledTasksMsg> list(@RequestBody Query query) {
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("create_time");
        }
        QueryWrapper<ScheduledTasksMsg> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new ScheduledTasksMsg()), entityList
        );
        TokenSearch<ScheduledTasksMsg> tokenSearch = new TokenSearch<>();
        tokenSearch.addTokenQuery(queryWrapper, query);
        queryWrapper.eq("is_deleted",0);
        IPage<ScheduledTasksMsg> pages = iScheduledTasksMsgService.page(Condition.getPage(query), queryWrapper);
        ListResult<ScheduledTasksMsg> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(pages.getRecords());
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        return listResult;
    }
    @PostMapping("/listAll")
    public ListResult<ScheduledTasksMsg> listAllByZgqk(@RequestBody Query query) {
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("create_time");
        }
        QueryWrapper<ScheduledTasksMsg> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new ScheduledTasksMsg()), entityList
        );
        TokenSearch<ScheduledTasksMsg> tokenSearch = new TokenSearch<>();
//        tokenSearch.addTokenQuery(queryWrapper, query);
        queryWrapper.eq("status",0);
        queryWrapper.eq("is_deleted",0);
        List<ScheduledTasksMsg> list = iScheduledTasksMsgService.list( queryWrapper);
        ListResult<ScheduledTasksMsg> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(list);
        listResult.setTotal(list.size());
        return listResult;
    }
    @PostMapping("/sureMsg")
    public R sureMsg(@RequestBody ScheduledTasksMsgDTO scheduledTasksMsgDTO){
        if(scheduledTasksMsgDTO!=null&& StringUtil.isNotBlank(scheduledTasksMsgDTO.getIds())){
            String[] splits= scheduledTasksMsgDTO.getIds().split(",");
            for (String str: splits) {
                if(StringUtil.isNotBlank(str)){
                    ScheduledTasksMsg byid=iScheduledTasksMsgService.getById(Long.parseLong(str));
                    if(byid!=null){
                        byid.setStatus(1);
                        byid.setUpdateTime(new Date());
                        iScheduledTasksMsgService.updateById(byid);
                    }
                }
            }
            return R.success();
        }else{
            return R.fail("请传入数据id");
        }

    }

    /**
     * 详情
     */
    @PostMapping("/detail")
    @Behavioral
    public  R<ScheduledTasksMsg> detail(@Valid @RequestBody ScheduledTasksMsg scheduledTasksMsg) {
        scheduledTasksMsg.setModuleType(moduleType);
        ScheduledTasksMsg detail = iScheduledTasksMsgService.getOne(Condition.getQueryWrapper(scheduledTasksMsg));
        R<ScheduledTasksMsg> objResult = new R<>(CommonCode.SUCCESS);
        objResult.setPageData(detail);
        return objResult;
    }
}
